class Solution
{
public:
    int maximumLength(vector<int> &nums, int k)
    {
        vector<vector<int>> dp(k, vector<int>(k, 0));
        int result = 0;
        for (int num : nums)
        {
            int r = num % k;

            vector<int> nextRow = dp[r];
            for (int m = 0; m < k; ++m)
            {
                int comp = (m + k - r) % k;
                nextRow[m] = max(nextRow[m], dp[comp][m] + 1);
                result = max(result, nextRow[m]);
            }
            dp[r] = nextRow;
        }
        return result;
    }
    int maximumLength_(vector<int> &nums, int k)
    {
        for (int &num : nums)
        {
            num %= k;
        }
        int result = 0;
        int n = nums.size();
        for (int m1 = 0; m1 < k; ++m1)
        {
            for (int m2 = 0; m2 < k; ++m2)
            {
                int l = 0;
                int m = m1;
                for (int num : nums)
                {
                    if (num == m)
                    {
                        ++l;
                        m = m1 + m2 - m;
                    }
                }
                result = max(result, l);
            }
        }
        return result;
    }
};